<?php
namespace Admin\Controller;
use Common\Model\CaozuologModel;
use Common\Model\ConfigModel;
class AuthController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	//**********************************************规则*******************************************
	/**
	 * 查询所有规则
	 */
	public function getAllRules(){
		$big = M('Auth_rule')->where(['pid'=>0])->select();
		foreach($big as &$v){
			$v['son'] = M('Auth_rule')->where(['pid'=>$v['id']])->select();
			$v['is_hidden']  = true;
		}
		$this->ajaxReturn(['status'=>1,'data'=>$big]);
	}
	/**
	 * 新增规则
	 */
	public function addRule(){
		$name = I('name');
		$title = I('title');
		$type = I('type');
		$pid = I('pid');
		$is_show = I('is_show');
		if(empty($pid)){
			$pid = 0;
		}
		if(empty($name)){
     		$data = [
				'status'=>-1,
				'msg'=>'请输入规则'
     		];
     		$this->ajaxReturn($data);
     	}
		if(empty($title)){
     		$data = [
				'status'=>-2,
				'msg'=>'请输入标题'
     		];
     		$this->ajaxReturn($data);
     	}
     	if(!empty($pid)){
     		$prule = M('Auth_rule')->where(['id'=>$pid])->field('id')->find();
     		if(!$prule){
     			$data = [
     			'status'=>-3,
     			'msg'=>'未查询到父级'
     					];
     			$this->ajaxReturn($data);
     		}
     	}
     	$rule = [
			'status'=>1,
			'name'=>$name,
			'title'=>$title,
			'type'=>'Admin',
			'pid'=>$pid,
			'is_show'=>1,
     	];
     	$r = M('Auth_rule')->add($rule);
     	if(!$r){
     		$data = [
     		'status'=>0,
     		'msg'=>'服务器繁忙'
     				];
     	
     	}else{
     		$data = [
     		'status'=>1,
     		'msg'=>'新增成功'
     				];
     	$M_LOG = new CaozuologModel();
     	$content = '新增规则，id为'.$r;
     	$M_LOG->addLog($this->user['username'], $content);
     	}
     	$this->ajaxReturn($data);
	}
	/**
	 * 修改权限
	 */
	public function changeRule(){
		$id = I('id');
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
		$this->ajaxReturn($data);
		}
		$rule = M('Auth_rule')->where(['id'=>$id])->field('id')->find();
		if(!$rule){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
		$this->ajaxReturn($data);
		}
		$name = I('name');
		$title = I('title');
		$type = I('type');
		$rule_save = [];
		if(!empty($name)){
			$rule_save['name']=$name;
		}
		if(!empty($title)){
			$rule_save['title']=$title;
		}
		if(!empty($type)){
			$rule_save['type']=$type;
		}
		$r = M('Auth_rule')->where(['id'=>$id])->save($rule_save);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'修改成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改规则，id为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 删除规则
	 */
	public function deleteRule(){
		$id = I('id');
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		$rule = M('Auth_rule')->where(['id'=>$id])->field('id')->find();
		if(!$rule){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		$r = M('Auth_rule')->where(['id'=>$id])->delete();
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'删除成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '删除规则，id为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	
	//*******************************************用户组*************************************************
	/**
	 * 获取所有的用户组
	 */
	public function getGroup(){
		$page = I('page');
		$num = I('num');
		$pages = $this->DoPage($page, $num);
		$group = M('Auth_group')->field('id,title,rules')->limit($pages['limit'],$pages['num'])->select();
		$count = M('Auth_rule')->count();
		$data = ['group'=>$group,'count'=>$count,'status'=>1];
		$this->ajaxReturn($data);
	}
	/**
	 * 根据id查询详细信息
	 */
	public function getGroupDetail(){
		$id = I('id');
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，缺少id'
					];
			$this->ajaxReturn($data);
		}
		$group = M('Auth_group')->where(['id'=>$id])->field('id,title,rules')->find();
		if(!$group){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，未查询到数据'
					];
			$this->ajaxReturn($data);
		}
		$rule = $group['rules'];
		$rules = explode(',', rtrim($rule,','));
		$rule_all = M('Auth_rule')->where(['pid'=>0])->select();
		foreach($rule_all as &$s){
			if(in_array($s['id'], $rules)){
				$s['is_have'] = true;
			}else{
				$s['is_have'] = false;
			}
			$s['son'] = M('Auth_rule')->where(['pid'=>$s['id']])->select();
			if($s['son']){
				foreach($s['son'] as &$v){
					if(in_array($v['id'], $rules)){
						$v['is_have'] = true;
					}else{
						$v['is_have'] = false;
					}
				}
			}
		}
		$this->ajaxReturn(['status'=>1,'data'=>$rule_all]);
	}
	/**
	 * 新增用户组
	 */
	public function addGroup(){
		$title = I('title');
		$rule = I('rule');
		
		if(empty($title)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入名称'
					];
			$this->ajaxReturn($data);
		}
		if(empty($rule)){
			$data = [
			'status'=>-2,
			'msg'=>'请选择权限'
					];
			$this->ajaxReturn($data);
		}
		$rules = explode(',', rtrim($rule,','));
		foreach ($rules as $v){
			if(!M('Auth_rule')->where(['id'=>$v])->field('id')->find()){
				$data = [
				'status'=>-2,
				'msg'=>'参数错误，有未录入的权限，请检查'
						];
				$this->ajaxReturn($data);break;
			}
		}
		$group = [
			'title'=>$title,
			'rules'=>$rule,
		];
		$r = M('Auth_group')->add($group);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'新增成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '新增用户组，id为'.$r;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 修改用户组
	 */
	public function changeGroup(){
		$id = I('id');
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，缺少id'
					];
			$this->ajaxReturn($data);
		}
		$group = M('Auth_group')->where(['id'=>$id])->field('id')->find();
		if(!$group){
			$data = [
			'status'=>-3,
			'msg'=>'参数错误，未查询到数据'
					];
			$this->ajaxReturn($data);
		}
		$title = I('title');
		$rule = I('rule');
		$group_save = [];
		if(!empty($title)){
			$group_save['title'] = $title;
		}
		if(!empty($rule)){
			$rules = explode(',', rtrim($rule,','));
			foreach ($rules as $v){
				if(!M('Auth_rule')->where(['id'=>$v])->field('id')->find()){
					$data = [
					'status'=>-2,
					'msg'=>'参数错误，有未录入的权限，请检查'
							];
					$this->ajaxReturn($data);break;
				}
			}
			$group_save['rules'] = $rule;
		}
		$r = M('Auth_group')->where(['id'=>$id])->save($group_save);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'修改成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改用户组，id为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 删除用户组
	 */
	public function deleteGroup(){
		$id = I('id');
		if(empty($id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，缺少id'
					];
			$this->ajaxReturn($data);
		}
		$group = M('Auth_group')->where(['id'=>$id])->field('id')->find();
		if(!$group){
			$data = [
			'status'=>-3,
			'msg'=>'参数错误，未查询到数据'
					];
			$this->ajaxReturn($data);
		}
		$check=M('Auth_group_access')->where('group_id ='.$id)->find();
		if($check){
			$data = [
			'status'=>-3,
			'msg'=>'该用户组存在管理员，操作失败'
					];
			$this->ajaxReturn($data);
		}
		$r = M('Auth_group')->where(['id'=>$id])->delete();
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'删除成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '删除用户组，id为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	//******************************************用户组权限联合******************************************
	/**
	 * 获取所有管理员
	 */
	public function getAllAdmin(){
		//查询条件
		//username status number  is_daili add_time
		$username = I('username');
		$status = I('status');
		$begin = I('begin');
		$last = I('last');
		$where = [];
		if(!empty($username)){
			$where['username'] = ['like','%'.$username.'%'];
		}
		if(!empty($status)){
			$where['status'] = $status;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$page = I('page');
		$num = I('num');
		$pages = $this->DoPage($page, $num);
		$admin = M('Admin')->where($where)->limit($pages['limit'],$pages['num'])->select();
		$count = M('Admin')->where($where)->count();
		foreach ($admin as &$v){
			$group_id = M('Auth_group_access')->field('group_id')->where(['uid'=>$v['admin_id']])->find();
			if($group_id){
				$juese = M('Auth_group')->field('id,title')->where(['id'=>$group_id['group_id']])->find();
				$v['wocao'] = (object) array('value' =>$juese['id'] ,'title'=>$juese['title'],'editStatus'=>false);
// 				$v['wocao'] = $juese;
			}else{
				$v['wocao'] = (object) array('value' =>'0' ,'title'=>'暂无','editStatus'=>false);
			}
			$v['username'] = (object) array('value' => $v['username'],'editStatus'=>false);
			$v['status'] = (object) array('value' => $v['status'],'editStatus'=>false);
		}
		$group = M('Auth_group')->select();
		$data = [
			'status'=>1,
			'data'=>$admin,
			'count'=>$count,
			'group'=>$group,
		];
		$this->ajaxReturn($data);
	}
	/**
	 * 新增管理员
	 */
	public function addAdminMember(){
		$username = I('username');
		$password = I('password');
		$password_t = I('password_t');
		if(empty($username)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，请输入正确的用户名'
					];
			$this->ajaxReturn($data);
		}
		if(M('Admin')->where(['username'=>$username])->find()){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，用户名已经存在'
					];
			$this->ajaxReturn($data);
		}
		if(empty($password)||empty($password_t)){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，请输入密码'
					];
			$this->ajaxReturn($data);
		}
		if($password!=$password_t){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，两次密码输入不一致'
					];
			$this->ajaxReturn($data);
		}
		if(!regex($password ,'password')){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，密码长度不符合标准'
					];
			$this->ajaxReturn($data);
		}
		$insert = [
			'username'=>$username,
			'password'=>md5($password),
			'status'=>1,
			'add_time'=>time(),
			'photo'=>'http://imgsrc.baidu.com/forum/w=580/sign=f8304c6a0c46f21fc9345e5bc6266b31/ff94b33533fa828b89e49f72fb1f4134950a5a81.jpg',
		];
		$r = M('Admin')->add($insert);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'新增成功'
					];
		}
		$M_LOG = new CaozuologModel();
		$content = '新增管理员，id为'.$r;
		$M_LOG->addLog($this->user['username'], $content);
		$this->ajaxReturn($data);
	}
	/**
	 * 生成四位数字
	 */
	public function getFourNumber(){
		for($i=0;$i<4;$i++) {
			$fontsize = 6;
			 
			$data = 'abcdefghijkmnpqrstuvwxy';
			$fontcontent = substr($data, rand(0, strlen($data)-1), 1);
			$captch_code .= $fontcontent;
		}
		return $captch_code;
	}
	/**
	 * 修改管理员的角色
	 */
	public function changeJuese(){
		$group_id = I('group_id');
		$admin_id = I('admin_id');
		if(empty($admin_id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，未获得管理员id'
					];
			$this->ajaxReturn($data);
		}
		if(empty($group_id)){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，未获得群组id'
					];
			$this->ajaxReturn($data);
		}
		if(!M('Admin')->where(['admin_id'=>$admin_id])->field('admin_id')->find()){
			$data = [
			'status'=>-3,
			'msg'=>'参数错误，未查询到此管理员'
					];
			$this->ajaxReturn($data);
		}
		if(!M('Auth_group')->where(['id'=>$group_id])->field('id')->find()){
			$data = [
			'status'=>-4,
			'msg'=>'参数错误，未查询到此群组'
					];
			$this->ajaxReturn($data);
		}
		$data = [
			'group_id'=>$group_id
		];
		$group_access = M('Auth_group_access')->where(['uid'=>$admin_id])->find();
		if($group_access){
			$r = M('Auth_group_access')->where(['uid'=>$admin_id])->save($data);
		}else{
			$data['uid'] = $admin_id;
			$r = M('Auth_group_access')->add($data);
		}
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'修改成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改admin_id为'.$admin_id.'用户所属用户组，修改为group_id为'.$group_id;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 
	 * 修改管理员信息
	 */
	public function changeInfo(){
		$username = I('username');
		$admin_id = I('admin_id');
		$password = I('password');
		$status = I('status');
		if(empty($admin_id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，缺少管理员id'
					];
			$this->ajaxReturn($data);
		}
		if(!empty($username)){
			if(M('Admin')->where(['username'=>$username])->find()){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，用户名已经存在',
						];
				$this->ajaxReturn($data);
			}
			$save['username'] = $username;
		}
		if(!empty($password)){
			if(!regex($password, 'password')){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，密码长度不符合标准',
				];
				$this->ajaxReturn($data);
			}
			$save['password'] = $password;
		}
		if(!empty($status)){
			if(!in_array($status, [1,-1])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，状态码错误',
				];
				$this->ajaxReturn($data);
			}
			$save['status'] = $status;
		}
		$r = M('Admin')->where('admin_id ='.$admin_id)->save($save);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'修改成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改admin_id为'.$admin_id.'用户信息';
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 重置管理员密码
	 */
	public function ResetAdminPwd(){
		$admin_id = I('admin_id');
		if(empty($admin_id)){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误，缺少管理员id'
					];
			$this->ajaxReturn($data);
		}
		if(!M('Admin')->where(['admin_id'=>$admin_id])->find()){
			$data = [
			'status'=>-2,
			'msg'=>'参数错误，没有查询到此管理员'
					];
			$this->ajaxReturn($data);
		}
		$M_CONFIG = new ConfigModel();
		$config = $M_CONFIG->getConfigToType();
		$r = M('Admin')->where(['admin_id'=>$admin_id])->setField('password',md5($config['admin_reset_pass']));
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'重置成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改admin_id为'.$admin_id.'用户密码';
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
}



